/**
 * @author 蒙伟明
 * @data 2020年6月14日
 */
package com.ysxm.controller.admincontroller;

import java.io.File;
import java.io.FileNotFoundException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.ysxm.entity.Comment;
import com.ysxm.entity.Movies;
import com.ysxm.entity.Moviessorrt;
import com.ysxm.service.adminservice.MoviesService;
import com.ysxm.service.userservice.UserService;
import com.ysxm.util.FileAccepterUtil;
import com.ysxm.util.ResponseInfo;
import com.ysxm.util.Ffmpeg;

/**
 * @author mwm
 *
 */
@Controller
@ResponseBody
public class MoviesController {
	@Autowired
	private MoviesService moviesService;
	@SuppressWarnings("unused")
	@Autowired
	private UserService userService;

	// 添加视频
	@RequestMapping(value = "addMovies", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")

	public ResponseInfo<String> addMovies(@RequestParam(value = "files", required = false) MultipartFile file,
			@Validated Movies movies, HttpServletRequest request, HttpServletResponse response) {
		/* 获取上传视频 */
		String path = request.getServletContext().getRealPath("/upload/");
		System.out.println("地址" + path);
		File video = null;
		//调用上传视频方法
		FileAccepterUtil fileAccepterUtil = new FileAccepterUtil();
		String sp = fileAccepterUtil.FileUpload(file, path);
		String fileName = file.getOriginalFilename();
		
		/*视频截取*/
		//获取视频名
		String newName = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()) + "."
				+ fileName.substring(fileName.lastIndexOf(".") + 1);
		try {
			video = ResourceUtils.getFile(path + newName);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		/*截图存放位置*/
		String picPath = path + newName + ".jpg";
		System.out.println("图片" + picPath);
		Ffmpeg.getVideoPic(video, picPath);
		// 返回长度
		@SuppressWarnings("unused")
		long duration = Ffmpeg.getVideoDuration(video);
		/*重新命名截图名字*/
		String newName2 = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()) + "."
				+ picPath.substring(picPath.lastIndexOf(".") + 1);
		String moviesaddress = sp;
		String moivecover = newName2;
		/* 存入数据库 */
		movies.setMid(movies.getMid());
		movies.setMoivecover(moivecover);
		movies.setMoviesaddress(moviesaddress);
		movies.toString();

		moviesService.addMovise(movies);
		return new ResponseInfo<String>(ResponseInfo.SUCCESS_CODE, "添加成功");
	}

	// 刪除電影
	@GetMapping(value = "delMovies", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResponseInfo<String> delMovies(@RequestBody Map<String, Object> maps) {
		@SuppressWarnings("unchecked")
		ArrayList<Integer> mids = (ArrayList<Integer>) maps.get("mid");
		for (Integer integer : mids) {
			System.out.println(integer);
		}
		Integer[] mid = mids.toArray(new Integer[mids.size()]);

		System.out.println(mid);
	

		moviesService.deleteMovise(mid);
		return new ResponseInfo<String>(ResponseInfo.SUCCESS_CODE, "删除成功");

	}

	// 管理员修改视频信息
	@RequestMapping(value = "updataMovies", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResponseInfo<Movies> updataMovies(@RequestParam(value = "file", required = false) MultipartFile file,
			Movies movies, HttpServletRequest request, HttpServletResponse response) {

		String path = request.getServletContext().getRealPath("/upload/");
		FileAccepterUtil accepterUtil = new FileAccepterUtil();
		String fileName = accepterUtil.FileUpload(file, path);
		movies.setMid(movies.getMid());
		movies.setMoviesaddress(fileName);
		movies.toString();

		moviesService.updateMovise(movies);
		return new ResponseInfo<Movies>(ResponseInfo.SUCCESS_CODE, "修改成功", movies);
	}

	// 管理員查看視頻信息
	@GetMapping(value = "findMovies", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResponseInfo<Movies> findMovies(Integer mid) {

		Movies movies = moviesService.findMoviseById(mid);

		return new ResponseInfo<Movies>(ResponseInfo.SUCCESS_CODE, "查看成功", movies);
	}

	// 管理員查看視頻分类
	@GetMapping(value = "findMoviesSort", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResponseInfo<List<Moviessorrt>> findMoviesSort(Integer msid) {

		List<Moviessorrt> msList = moviesService.findMoviessortByMsid(msid);
		return new ResponseInfo<List<Moviessorrt>>(ResponseInfo.SUCCESS_CODE, "查看成功", msList);
	}

	// 管理員显示所有視頻信息
	@GetMapping(value = "afindAllMovies", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResponseInfo<PageInfo<Movies>> findAllMovies(
			@RequestParam(name = "pageNum", required = true, defaultValue = "1") int pageNum,
			@RequestParam(name = "pageSize", required = true, defaultValue = "10") int pageSize, HttpSession session,
			PageInfo<Movies> pageInfo) {

		List<Movies> msList = moviesService.findAllMovise(pageSize, pageSize);
		pageInfo = new PageInfo<Movies>(msList);
		return new ResponseInfo<PageInfo<Movies>>(ResponseInfo.SUCCESS_CODE, "查看成功", pageInfo);
	}

	// 管理员通过搜索框查询影视
	@GetMapping(value = "checkMovies", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResponseInfo<List<Movies>> checkMovies(String moviesname,String moviesdescribe) {
System.out.println(moviesdescribe);
		List<Movies> mList = moviesService.checkMovies(moviesname,moviesdescribe);
		return new ResponseInfo<List<Movies>>(ResponseInfo.SUCCESS_CODE, "搜素成功", mList);
	}

	// 管理員显示未审核评论
	@GetMapping(value = "afindAllComment", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResponseInfo<List<Comment>> findAllComment() {

		List<Comment> cmsList = moviesService.findAllComment();

		return new ResponseInfo<List<Comment>>(ResponseInfo.SUCCESS_CODE, "显示成功", cmsList);
	}

	// 批量评论不通过
	@PostMapping(value = "delComment", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResponseInfo<String> delComment(@RequestBody Map<String, Object> maps) {
		@SuppressWarnings("unchecked")
		ArrayList<Integer> cids = (ArrayList<Integer>) maps.get("cid");
		for (Integer integer : cids) {
			System.out.println(integer);
		}
		Integer[] cid = cids.toArray(new Integer[cids.size()]);

		System.out.println(cid);
		Boolean result = moviesService.deleteComment(cid);
		System.out.println(result);
		return new ResponseInfo<String>(ResponseInfo.SUCCESS_CODE, "批量删除成功");
	}

	// 批量评论通过
	@PostMapping(value = "passComment", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResponseInfo<String> passComment(@RequestBody Map<String, Object> maps) {
		@SuppressWarnings("unchecked")
		ArrayList<Integer> cids = (ArrayList<Integer>) maps.get("cid");
		for (Integer integer : cids) {
			System.out.println(integer);
		}
		Integer[] cid = cids.toArray(new Integer[cids.size()]);
		@SuppressWarnings("unused")
		Boolean result = moviesService.passComment(cid);

		return new ResponseInfo<String>(ResponseInfo.SUCCESS_CODE, "批量通过成功");
	}

}
